<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>LB-Admin1.0</title>
    {include file="header"}
    <style>
        .menu {
            background-color: #f1f1f1;
            padding: 10px;
            border-bottom: 4px solid #fff;
            font-size: 16px;
            min-height: 60px;
        }

        .menu input {
            width: 16px;
            height: 16px;
        }
        .menu1 {font-weight:bold;
        }
        .menu2 {
            margin-left: 20px;float:left;
        }

        .menu3 {
            margin-left: 40px;
            display: inline-block;
        }

        .menu3 .menu3-item {
            margin-right: 10px;
            float: left;
            min-width: 120px;
        }

        .rolename {
            font-size: 16px;
        }
    </style>
</head>

<body>
    <div class="x-nav">
        <div class="layui-breadcrumb" style="float: left;">
            <!--<a onclick="history.back()"><i class="layui-icon layui-icon-prev"></i></a>-->
            <a>首页</a>
            <a>权限设置</a>
        </div>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body layui-table-body layui-table-main">
                        <div class="think-box-shadow">
                            <div class="rolename">
                                <label>权限组：</label>{$role.title}
                            </div>
                            {empty name='menu'}
                            <blockquote class="layui-elem-quote">没 有 记 录 哦！</blockquote>
                            {else}                            
                                {foreach $menu as $v}
                                <div class="menu">
                                    <div class="menu1">
                                        <input type="checkbox" name="power_id" id="p1_{$v.id}" value="{$v.id}" class="node-choice" {if $v.read==1}checked{/if} />
                                        <span>{$v.title}</span>
                                    </div>
                                    {foreach $v.child as $vv}
                                    <div class="menu2">
                                        <input type="checkbox" name="power_id" id="p2_{$v.id}_{$vv.id}" value="{$vv.id}" class="node-choice" {if $vv.read==1}checked{/if} />
                                        <span>{$vv.title}</span>
                                    </div>
                                    {/foreach}
                                </div>
                                <div class="clear"></div>
                                {/foreach}
                            <div class="layui-row"><button class="layui-btn layui-btn-normal layui-btn-lg" onclick="save()">保存</button>
                            <button class="layui-btn layui-btn-disable layui-btn-lg" onclick="history.back()">返回</button>
                            </div>
                            {/empty}
                        </div>
                        <!--list end-->
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        $(function(){
            $("input[id^='p1_']").click(function(){
                var id=$(this).attr('id');
                id=id.split('_')[1];
                
                if($(this).is(":checked"))
                {                    
                    $("input[id^='p2_"+id+"_']").each(function(){
                        $(this).prop("checked",true)
                    })
                }
                else
                {
                    $("input[id^='p2_"+id+"_']").each(function(){                        
                        $(this).prop("checked",false)
                    })
                }
            })
            $("input[id^='p2_']").click(function(){
                var vid=$(this).attr('id');
                id=vid.split('_')[1];
                var id3=vid.split('_')[2];
                console.log(id)
                var flag=$(this).is(":checked");
                if(flag)
                {                    
                    $("input[id='p1_"+id+"']").prop("checked",true)
                }
                else
                {
                    var is_check_all=false;                    
                    $("input[id^='p2_"+id+"_']").each(function(){
                        if($(this).is(":checked"))
                        {
                            is_check_all=true;
                        }
                    })
                    if(!is_check_all)
                    {
                        $("input[id='p1_"+id+"']").prop("checked",false)
                    }
                }
            })
          
        })
        function update_auth(role_id,node_id,auth)
        {
            $.post('{:url('saveauth')}', {id:role_id, node_id:node[3],auth:auth}, function(res) {
                console.log(res)
                if (res.code == 1) {
                    layer.msg('成功', { icon: 1, time: 500 }, function () {
                        parent.location.reload();
                        parent.layer.close();
                    })
                    return
                }
                layer.msg('失败')
            })
        }
        function check_parent(node)
        {
            var p2=node[1]+"_"+node[2];
            var is_check=false;
            $("input[id^='p2_"+node[1]+"_"+node[2]+"']").each(function(){
                if($(this).is(":checked"))
                {
                    is_check=true;
                }
            })
            if(!is_check)
            {
                var p1_id=node[1]
                $("#p1_"+p1_id).attr("checked",false)
               
            }
        }
        var role_id = {$role.id};
        function save(id) {
            var ids=[];
            $("input[id^='p']").each(function(){
                if($(this).is(":checked"))
                {
                    ids.push($(this).val())
                }
            })    
            var formData={ id:ids, role_id:role_id};
            console.log(formData)
            //return
            $.post('{:url('saveauth')}', formData, function (res) {
                console.log(res)
                if (res.code == 1) {
                    layer.msg('成功', { icon: 1, time: 500 }, function () {
                        parent.location.reload();
                parent.layer.close();
                    })
                    return
                }
                layer.msg('失败')
            })
        }
    </script>
    {include file="footer"}